package com.hualan.sfzy.controller.permission;

import com.hualan.sfzy.model.entity.SystemUsers;
import com.hualan.sfzy.model.entity.UserRole;
import com.hualan.sfzy.service.impl.permission.SystemUsersServiceImpl;
import com.hualan.sfzy.service.impl.permission.UserRoleServiceImpl;
import com.hualan.sfzy.utils.MD5Util;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private SystemUsersServiceImpl service;
    @Autowired
    private UserRoleServiceImpl userRoleService;
    @GetMapping
    public Result login(String username, String userpwd) {
        SystemUsers users = service.findByUsername(username);

        if(users==null){
            return Result.error("用户不存在");
        }

        Integer id = users.getId();
        UserRole role = userRoleService.findById(id);
        if(role==null){
            return Result.error("用户角色关系不存在");
        }
        if(role.getRoleId()==2){
            return Result.error("您没有权限登录后台");
        }

        String pwd = MD5Util.encode(userpwd);
        if(!users.getUserpwd().equals(pwd)){
            return Result.error("用户名或密码错误");
        }

        return Result.success(users);

    }


    @GetMapping("/user")
    public Result user(String username, String userpwd) {
        SystemUsers users = service.findByUsername(username);

        // 判断用户是否存在
        if(users==null){
            return Result.error("用户不存在");
        }

        // 判断用户角色关系是否存在
        Integer id = users.getId();
        UserRole role = userRoleService.findById(id);

        System.out.println( role.getRoleId());
        // 判断角色是否是 用户
        if(role.getRoleId()==2){
            // 判断密码是否正确
            String pwd = MD5Util.encode(userpwd);
            if(!users.getUserpwd().equals(pwd)){
                return Result.error("用户名或密码错误");
            }else return Result.success(users);
        }else {
            return Result.error("请注册用户");
        }
//        return Result.success(users);
    }


    // 根据用户id查找密码然后比对，密码正确则修改密码为第二个，否则密码错误
    @GetMapping("/updatePwd")
    @Transactional
    public Result updatePwd(Integer id, String oldPwd, String newPwd) {
        SystemUsers users = service.findById(id);
        if(users==null){
            return Result.error("用户不存在");
        }else{
            String pwd = MD5Util.encode(oldPwd);
            if(users.getUserpwd().equals(pwd)){
                String newPwd1 = MD5Util.encode(newPwd);
                users.setUserpwd(newPwd1);
                service.edit(users);
                return Result.success("修改密码成功");
            }else{
                return Result.error("密码错误");
            }
        }
    }

    // 用户忘记密码
    @GetMapping("/forgetPwd")
    @Transactional
    public Result forgetPwd(String mobile, String email, String salt) {

        SystemUsers users = new SystemUsers();
        users.setMobile(mobile);
        users.setEmail(email);
        users.setSalt(salt);
        SystemUsers users1 = service.forGetPwd(users);
        return users1==null?Result.error("您输入的有误"):Result.success(users1.getUsername());


    }
}
